package usables;

//import org.xml.sax.*;
//import org.xml.sax.helpers.*;
//import org.xml.sax.InputSource;
//import handler.ImageHandler;
//import java.io.*;
import java.util.*;
import org.xmldb.api.base.*;
import org.xmldb.api.base.Collection;
//import org.xmldb.api.modules.*;
import org.xmldb.api.*;
import org.exist.xmldb.XQueryService;
import structures.Image;
import structures.Video;
import structures.Notice;
import structures.User;
import structures.Query;

/**
 * Using Image Hanler Test Class
 * @author Jose Antonio Prieto
 */
public class Handler {

	/** Image Map */
	@SuppressWarnings("unused")
	private Map < String, Image > image;
	/** Video Map */
	@SuppressWarnings("unused")
	private Map < String, Video > video;
	/** Notice Map */
	@SuppressWarnings("unused")
	private Map < String, Notice > notice;
	/** User Map */
	@SuppressWarnings("unused")
	private Map < String, User > user;
	/** Driver */
	private String driver;
	/** URI */
	private String URI;
	
	/**
	 * Constructor
	 * @param driver
	 * @param URI
	 */
	public Handler(String driver,String URI){
		image = new HashMap<String, Image>();
		video = new HashMap<String, Video>();
		notice = new HashMap<String, Notice>();
		user = new HashMap<String, User>();
		this.driver = driver;
		this.URI = URI;
	}
	
	/**
	 * Register to the Database
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public void registerDatabase()throws Exception{
		Class cl = Class.forName(driver);
        Database database = (Database) cl.newInstance();
        DatabaseManager.registerDatabase(database);
	}
	
	/**
	 * Perform a query
	 * @param Query
	 * @return ResourceSet with the results
	 * @throws Exception
	 */
	public ResourceSet performQuery(String Query) throws Exception {
		// Obtain the collection and launch the query, obtaining a set o results. 	    
        Collection col = DatabaseManager.getCollection(URI);
        XQueryService service = (XQueryService) col.getService("XQueryService", "1.0");
        Query qry = new Query();
        qry.loadQuerys();
        ResourceSet result = service.query(Query);
        return result;
	}
	
//    public static void main(String[] args) throws Exception {
//
//
//        // Iterate through the set of results, which are XML elements of type photo 
//        // (note that the resources are taken as XMLResources!). 
//        // Each XMLResource is processed by the SAX handler (we provide the handler 
//        // with a new Photo object so it can store the parsed information).
//        // The obtained objects are stored in the HashMap images. 
//        ResourceIterator i = result.getIterator();
//        while (i.hasMoreResources()) {
//            XMLResource r = (XMLResource) i.nextResource();
//            Image ph = new Image();
//            ImageHandler handler = new ImageHandler(ph);
//            r.getContentAsSAX(handler);
//            images.put(ph.getId(), ph);
//        }
//
//        // Print the photo objects we have obtained
//        List<String> phs = new ArrayList<String>(images.keySet());
//        Collections.sort(phs);
//        for (Iterator iterator = phs.iterator(); iterator.hasNext();) {
//            String id = (String) iterator.next();
//            System.out.println(images.get(id));
//        }
//    }
}
